package com.gmako.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.gmako.entity.Homeimg;
import com.gmako.service.HomeImgService;

@Controller
@RequestMapping("/homeImg.do")
public class HomeImgController{
	@Autowired(required=false)
	private HomeImgService HomeImgService;
	/**
	 * 添加图片
	 * 
	 * @author qifu
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=addHomeImg")
	public ModelAndView addHouseInfoView(HttpServletRequest request){
		List<Homeimg> list = HomeImgService.findAll();
		return new ModelAndView("admin/houseImg","list",list);
	}
	/**
	 * 上传图片
	 * 
	 * @author qifu
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping()
	public void upload(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String path = getRealPath(request, "images/homeimg/");
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdir();
		}
		MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
		MultipartFile file = multipart.getFile("Filedata");
		if (file == null || file.isEmpty()) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST);
		}
		String newFileName = generateId();
		String y_fileName = file.getOriginalFilename();
		String extname = file.getOriginalFilename().substring(
		y_fileName.lastIndexOf("."));
		String fileName = request.getParameter("imgid")+extname;
		String fromFileStr = dir.getAbsolutePath() + File.separator + fileName;
		FileCopyUtils.copy(file.getBytes(), new File(dir.getAbsolutePath()
				+ File.separator + fileName));
		Homeimg homeimg = new Homeimg();
		homeimg.setId(Integer.parseInt(request.getParameter("imgid")));
		//homeimg.setImgurl("images/homeimg/"+fileName);
		homeimg.setImgurl(fileName);
		HomeImgService.update(homeimg);
		//HomeImgService.addImg(homeimg);
		response.getWriter().print(fileName);
		request.setAttribute("newFileName",fileName);
	}
	/**
	 * 删除图片
	 * 
	 * @author qifu
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=delimg")
	public ModelAndView delImg(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HomeImgService.delAll();
		return new ModelAndView("admin/houseImg","info","添加成功");

	}
	/**
	 * 获取图片路径
	 * 
	 * @author qifu
	 * @param request
	 * @param path
	 * @return
	 */
	public String getRealPath(HttpServletRequest request, String path) {
		return request.getSession().getServletContext().getRealPath(path);
	}
	/**
	 * 生成图片id
	 * 
	 * @author qifu
	 * @param request
	 * @param path
	 * @return
	 */
	public static String generateId() {
		return new Date().getTime() + getRandomString(5);
	}
	/**
	 * 生成随机字符串(数字+字母)
	 * @author qifu
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(3);
			long result = 0;
			switch (number) {
			case 0:
				result = Math.round(Math.random() * 25 + 65);
				sb.append(String.valueOf((char) result));
				break;
			case 1:
				result = Math.round(Math.random() * 25 + 97);
				sb.append(String.valueOf((char) result));
				break;
			case 2:
				sb.append(String.valueOf(new Random().nextInt(10)));
				break;
			}
		}
		return sb.toString();
	}
}
